package com.basics.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basics.api.request.channel.GroupChannelMemberReq;
import com.basics.api.request.organization.GroupOrganizationMemberReq;
import com.basics.api.response.organization.GroupOrganizationMemberListData;
import com.basics.api.response.organization.OrganizationMemberListData;
import com.basics.channel.entity.CyChannel;
import com.basics.channel.entity.CyChannelGroup;
import com.basics.channel.entity.CyChannelGroupUser;
import com.basics.channel.entity.CyChannelMsg;
import com.basics.channel.mapper.CyChannelGroupMapper;
import com.basics.channel.mapper.CyChannelGroupUserMapper;
import com.basics.channel.mapper.CyChannelMapper;
import com.basics.channel.service.CyChannelGroupUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basics.organization.entity.CyOrganizationUser;
import com.basics.organization.mapper.CyOrganizationUserMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 频道用户绑定表 服务实现类
 * </p>
 *
 * @author WangWei
 * @since 2025-03-09
 */
@Service
public class CyChannelGroupUserServiceImpl extends ServiceImpl<CyChannelGroupUserMapper, CyChannelGroupUser> implements CyChannelGroupUserService {

    @Resource
    private CyOrganizationUserMapper organizationUserMapper;
    @Resource
    private CyChannelMapper cyChannelMapper;
    @Autowired
    private CyChannelGroupMapper cyChannelGroupMapper;

    @Override
    public CyChannelGroupUser getByChannelId(Long channelId, Long userId){
        return getOne(new QueryWrapper<CyChannelGroupUser>().eq("channel_id",channelId).eq("user_id",userId));
    }

    @Override
    public int getMemberByGroupId(String groupId){
        return count(new QueryWrapper<CyChannelGroupUser>().eq("group_id",groupId).eq("del_flag",0));
    }

    @Override
    @Transactional
    public void addAllUserByOrganizationId(Long organizationId,Long channelId){
        List<CyOrganizationUser> cyOrganizationUsers = organizationUserMapper.selectList(new QueryWrapper<CyOrganizationUser>()
                .eq("organization_id",organizationId).eq("del_flag",0));
        if(CollectionUtils.isNotEmpty(cyOrganizationUsers)){
            return;
        }
        CyChannelGroup group = cyChannelGroupMapper.selectOne(new QueryWrapper<CyChannelGroup>()
                .eq("del_flag",0).eq("channel_id",channelId).eq("default_flag",1));
        List<CyChannelGroupUser> cyChannelGroupUsers = new ArrayList<>();
        for (CyOrganizationUser cyOrganizationUser : cyOrganizationUsers) {
            CyChannelGroupUser cyChannelGroupUser = new CyChannelGroupUser();
            cyChannelGroupUser.setChannelId(channelId);
            cyChannelGroupUser.setUserId(cyOrganizationUser.getUserId());
            cyChannelGroupUser.setDisturbFlag(0);
            cyChannelGroupUser.setTopFlag(0);
            if(group!=null){
                cyChannelGroupUser.setGroupId(group.getId());
            }
            cyChannelGroupUsers.add(cyChannelGroupUser);
        }
        saveBatch(cyChannelGroupUsers);
    }

    @Override
    public void addAllChannelByOrganizationId(Long organizationId,Long userId){
        List<CyChannel> channelList = cyChannelMapper.selectList(new QueryWrapper<CyChannel>()
                .eq("organization_id",organizationId).eq("del_flag",0));
        if(CollectionUtils.isEmpty(channelList)){
            return;
        }
        List<CyChannelGroupUser> joinList = list(new QueryWrapper<CyChannelGroupUser>().eq("user_id",userId));
        HashMap<Long,Boolean> joinMap  = new HashMap<>();
        if(CollectionUtils.isNotEmpty(joinList)){
            for (CyChannelGroupUser cyChannelGroupUser : joinList) {
                joinMap.put(cyChannelGroupUser.getChannelId(),true);
            }
        }
        List<CyChannelGroup> cyChannelGroupList = cyChannelGroupMapper.selectList(new QueryWrapper<CyChannelGroup>()
                .eq("del_flag",0).eq("organization_id",organizationId));
        HashMap<Long,CyChannelGroup>  groupHashMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(cyChannelGroupList)){
            for (CyChannelGroup cyChannelGroup : cyChannelGroupList) {
                groupHashMap.put(cyChannelGroup.getChannelId(),cyChannelGroup);
            }
        }

        List<CyChannelGroupUser> cyChannelGroupUsers = new ArrayList<>();
        for (CyChannel cyChannel : channelList) {
            if(!joinMap.containsKey(cyChannel.getId())){
                CyChannelGroupUser cyChannelGroupUser = new CyChannelGroupUser();
                cyChannelGroupUser.setChannelId(cyChannel.getId());
                cyChannelGroupUser.setUserId(userId);
                cyChannelGroupUser.setDisturbFlag(0);
                cyChannelGroupUser.setTopFlag(0);
                if(groupHashMap.containsKey(cyChannel.getId())){
                    cyChannelGroupUser.setGroupId(groupHashMap.get(cyChannel.getId()).getId());
                }
                cyChannelGroupUsers.add(cyChannelGroupUser);
            }

        }
        if(CollectionUtils.isNotEmpty(cyChannelGroupUsers)){
            saveBatch(cyChannelGroupUsers);
        }

    }

    @Override
    public IPage<GroupOrganizationMemberListData> selectChannelGroupMemberList(GroupChannelMemberReq req){
        return baseMapper.selectChannelGroupMemberList(new Page(req.getPageNum(),req.getPageSize())
                ,req.getChannelId(),req.getSearchText());
    }

    @Override
    public long getGroupMemberNum(String groupId){
        return count(new QueryWrapper<CyChannelGroupUser>().eq("group_id",groupId).eq("del_flag",0));
    }

    @Override
    public  long getJoinMemberNum(Long channelId){
        return count(new QueryWrapper<CyChannelGroupUser>().eq("channel_id",channelId).eq("del_flag",0));
    }

    @Override
    public IPage<OrganizationMemberListData> selectMemberListByGroupId(Page page, String groupId){
        return baseMapper.selectMemberListByGroupId(page,groupId);
    }
}
